var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all) __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if ((from && typeof from === 'object') || typeof from === 'function') {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, {
          get: () => from[key],
          enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
        });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (
  (target = mod != null ? __create(__getProtoOf(mod)) : {}),
  __copyProps(
    // If the importer is in node compatibility mode or this is not an ESM
    // file that has been converted to a CommonJS file using a Babel-
    // compatible transform (i.e. "__esModule" has not been set), then set
    // "default" to the CommonJS "module.exports" for node compatibility.
    isNodeMode || !mod || !mod.__esModule
      ? __defProp(target, 'default', { value: mod, enumerable: true })
      : target,
    mod,
  )
);
var __toCommonJS = (mod) => __copyProps(__defProp({}, '__esModule', { value: true }), mod);

// src/DingQrCode/index.tsx
var DingQrCode_exports = {};
__export(DingQrCode_exports, {
  default: () => DingQrCode,
});
module.exports = __toCommonJS(DingQrCode_exports);
var import_icons = require('@ant-design/icons');
var import_antd = require('antd');
var import_react = __toESM(require('react'));
var import_index = require('./index.less');
function DingQrCode({ appId, redirectUrl, pure, iframeStyle, onGetCode }) {
  const searchParams = new URLSearchParams(location.search);
  const code = searchParams.get('code');
  const [timestamp, setTimestamp] = (0, import_react.useState)(
    /* @__PURE__ */ new Date().getTime(),
  );
  (0, import_react.useEffect)(() => {
    if (code) onGetCode(code);
  }, []);
  (0, import_react.useEffect)(() => {
    const handleLogin = (event) => {
      if (event.origin === 'https://login.dingtalk.com') {
        const loginTmpCode = event == null ? void 0 : event.data;
        if (loginTmpCode) {
          window.location.href = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${appId}&response_type=code&scope=snsapi_login&state=${/* @__PURE__ */ new Date().getTime()}&redirect_uri=${encodeURIComponent(
            redirectUrl,
          )}&loginTmpCode=${loginTmpCode}`;
        } else {
          setTimestamp(/* @__PURE__ */ new Date().getTime());
        }
      }
    };
    window.addEventListener('message', handleLogin);
    return () => {
      window.removeEventListener('message', handleLogin);
    };
  }, [appId, redirectUrl]);
  const iframeSrc = (0, import_react.useMemo)(() => {
    const params = new URLSearchParams();
    const goto = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${appId}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${encodeURIComponent(
      redirectUrl,
    )}`;
    params.set('goto', goto);
    params.set('style', 'border:none;background-color:#FFFFFF;');
    return 'https://login.dingtalk.com/login/qrcode.htm?' + params.toString();
  }, [appId, redirectUrl, timestamp]);
  return code
    ? /* @__PURE__ */ import_react.default.createElement(
        'div',
        {
          style: {
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
            height: '100vh',
          },
        },
        /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, {
          tip: '钉钉登录中...',
        }),
      )
    : /* @__PURE__ */ import_react.default.createElement(
        'div',
        { className: 'szzj-ding-qrcode' },
        /* @__PURE__ */ import_react.default.createElement(
          'div',
          { className: 'szzj-ding-qrcode-iframe', style: iframeStyle },
          /* @__PURE__ */ import_react.default.createElement('iframe', {
            src: iframeSrc,
            style: iframeStyle,
          }),
        ),
        !pure &&
          /* @__PURE__ */ import_react.default.createElement(
            'div',
            { className: 'szzj-ding-qrcode-tips' },
            /* @__PURE__ */ import_react.default.createElement(
              'div',
              {
                style: {
                  display: 'flex',
                },
              },
              /* @__PURE__ */ import_react.default.createElement(
                'div',
                { className: 'szzj-ding-qrcode-tips-icon' },
                /* @__PURE__ */ import_react.default.createElement(import_icons.ScanOutlined, {
                  style: { fontSize: '40px' },
                }),
              ),
              /* @__PURE__ */ import_react.default.createElement(
                'div',
                { className: 'szzj-ding-qrcode-tips-note' },
                '打开',
                /* @__PURE__ */ import_react.default.createElement('span', null, '钉钉 APP'),
                /* @__PURE__ */ import_react.default.createElement('br', null),
                '扫码登录',
              ),
            ),
          ),
      );
}
